﻿using System;
using System.Reflection;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;

namespace DTFramework.Procedure
{
    public class ProcedureHotFix : ProcedureBase
    {
        private Type m_procedureEntry;
        private bool m_isNextEntry = false;

        protected override void OnEnter(ProcedureOwner procedureOwner)
        {
            base.OnEnter(procedureOwner);

            var assemblyName = GameEntry.BuiltinData.BuiltinConfig.m_ProjectAssembly;
            var assembly = Assembly.Load(assemblyName);

            if (assembly == null)
            {
                throw new Exception("==> 流程入口进入失败，找不到程序集！");
            }
            var procedureEntry = GameEntry.BuiltinData.BuiltinConfig.m_ProjectProcedureEntry;
            m_procedureEntry = assembly.GetType(procedureEntry);
            if (m_procedureEntry == null)
            {
                throw new Exception("==> 流程入口进入失败，找不到入口类型！");
            }
            m_isNextEntry = true;
        }
        protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
            if (!m_isNextEntry) { return; }
            ChangeState(procedureOwner, m_procedureEntry);
        }

        protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
        {
            base.OnLeave(procedureOwner, isShutdown);
        }
    }
}